/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Microsoft.Practices.RecipeFramework;

namespace DslEditorPowerToy.ComponentModel.Design
{
    /// <summary>
    /// A reference that determines whether to pick certain solution items
    /// </summary>
    internal class SolutionItemPickerReference : UnboundRecipeReference
    {
        private const char FilterDelimiter = ';';

        private string _filter = string.Empty;
        private SolutionItemType _type = SolutionItemType.Custom;

        #region Constructors
        public SolutionItemPickerReference()
            : base(string.Empty)
        {
        }
        #endregion

        #region Public Properties
        public override string AppliesTo
        {
            get
            {
                return "Configured itemtype";
            }
        }
        public String Filter
        {
            get
            {
                return this._filter;
            }
            set
            {
                this._filter = value;
            }
        }
        internal SolutionItemType ItemType
        {
            get
            {
                return this._type;
            }
            set
            {
                this._type = value;
            }
        }
        #endregion

        #region Public Methods
        public override bool IsEnabledFor(object target)
        {
            if (target == null)
            {
                System.Diagnostics.Debug.WriteLine("Null Target");
                return false;
            }
                        
            //We need to check for first item (solution) otherwise other items never shown, 
            // becuase solution is always first selected item
            if (target is EnvDTE.Solution)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Solution Target: {0}", (target as EnvDTE.Solution).FullName));
                if (0 != (this._type & SolutionItemType.Solution))
                    return true;
                else
                {
                    if (0 != (this._type & SolutionItemType.Custom))
                        return false;
                }
            }
            if (0 != (this._type & SolutionItemType.Custom))
            {
                //Always valid selection (override to provide processing logic)
                return true;
            }

            //Check solution item types for configured values
            if (target is EnvDTE80.SolutionFolder)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("SolutionFolder Target: {0}", (target as EnvDTE80.SolutionFolder).Parent.FullName));
                if (0 != (this._type & SolutionItemType.SolutionFolder))
                    return true;
            }
            if (target is EnvDTE.Project)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Project Target: {0}", (target as EnvDTE.Project).FullName));
                if (0 != (this._type & SolutionItemType.Project))
                    return true;
            }
            if (target is EnvDTE.ProjectItem)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("ProjectItem Target: {0}", (target as EnvDTE.ProjectItem).Name));
                EnvDTE.ProjectItem item = target as EnvDTE.ProjectItem;

                if (true == item.Kind.Equals(EnvDTE.Constants.vsProjectItemKindPhysicalFolder))
                {
                    if (0 != (this._type & SolutionItemType.ProjectFolder))
                        return true;
                }
                if (true == item.Kind.Equals(EnvDTE.Constants.vsProjectItemKindPhysicalFile))
                {
                    if (0 != (this._type & SolutionItemType.File))
                    {
                        if (true == string.IsNullOrEmpty(this._filter))
                            return true;
                        else
                        {
                            //Check file extension
                            string fileExtension = Path.GetExtension(item.Name).Replace(".", string.Empty);

                            // Note: filter can be "bmp;gif;jpg;cs"
                            string[] extensions = this._filter.Split(new char[] { FilterDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string extension in extensions)
                            {
                                if (0 == string.Compare(extension, fileExtension, true))
                                    return true;
                            }
                        }
                    }
                }
            }

            return false;
        }
        #endregion
    }
}
